<template>
	<div class="body">
		<div class="container">
			<div id="from-box" class="form-box">
				<div id="register-box" class="register-box hidden">
					<h1>register</h1>
					<input
						type="text"
						placeholder="账号"
						v-model="regForm.userId"
					/>
					<input
						type="password"
						placeholder="密码"
						v-model="regForm.password"
					/>
					<input type="password" placeholder="确认密码" v-model="regForm.re_password" />
					<button @click="reg">注册</button>
				</div>
				<div id="login-box" class="login-box">
					<h1>login</h1>
					<input
						type="text"
						placeholder="账号"
						v-model="loginForm.userId"
					/>
					<input
						type="password"
						placeholder="密码"
						v-model="loginForm.password"
					/>
					<button @click="login">登录</button>
				</div>
			</div>
			<div class="con-box left">
				<h2>欢迎来到一面</h2>
				<img src="@\assets\images\人脸识别.png" alt="" />
				<p>已有账号</p>
				<button @click="goLogin">去登录</button>
			</div>
			<div class="con-box right">
				<h2>欢迎来到一面</h2>
				<img src="@\assets\images\人脸识别.png" alt="" />
				<p>没有账号？</p>
				<button @click="goRegister">去注册</button>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { reqLogin, reqReg } from '@/api/user/userInfo'
import useUserStore from '@/store/modules/user'
import{getPermissionByUserId}from '@/utils/getPermissionNameByUserId'
let router = useRouter()
//登录注册表单
const loginForm = reactive({ userId: '', password: '' })
const regForm = reactive({ userId: '', password: '', re_password: '' })
const userStore = useUserStore()
//登录
const login = async () => {
	console.log(loginForm.userId, loginForm.password)
	const res = await reqLogin(loginForm.userId, loginForm.password)
	if (res.data.isSuccess == false) {
		alert('账号或密码错误')
		return
	} else {
		userStore.userId = loginForm.userId
        getPermissionByUserId(loginForm.userId)
		userStore.userId=loginForm.userId
		router.push('/home')
	}
}
//注册
const reg = async () => {
		const res = await reqReg(regForm.userId, regForm.password)
		if(res.data.isSuccess==false){
			alert('账号已存在')
			return
		}
		router.push('/login')
	
}

// 实现登录界面滑动功能
function goRegister() {
	let form_box = document.getElementById('from-box') as HTMLElement
	let register_box = document.getElementById(
		'register-box'
	) as HTMLElement
	let login_box = document.getElementById('login-box') as HTMLElement
	form_box.style.transform = 'translateX(80%)'
	login_box.classList.add('hidden')
	register_box.classList.remove('hidden')
}
function goLogin() {
	let form_box = document.getElementById('from-box') as HTMLElement
	let register_box = document.getElementById(
		'register-box'
	) as HTMLElement
	let login_box = document.getElementById('login-box') as HTMLElement
	form_box.style.transform = 'translateX(0%)'
	login_box.classList.remove('hidden')
	register_box.classList.add('hidden')
}
</script>

<style>
* {
	margin: 0;
	padding: 0;
}
.body {
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	background: linear-gradient(200deg, #f3e7e9 0%, #e3eeff 100%);
}
.container {
	width: 650px;
	height: 415px;
	background-color: #fff;
	border-radius: 5px;
	box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
	position: relative;
}
.form-box {
	position: absolute;
	top: -10%;
	left: 5%;
	background-color: #d3b7d8;
	width: 320px;
	height: 500px;
	border-radius: 5px;
	box-shadow: 2px 0px 10px rgba(0, 0, 0, 0.1);
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 2;
	transition: 0.5s ease-in-out;
}
.register-box,
.login-box {
	display: flex;
	flex-direction: column;
	align-items: center;
	width: 100%;
}
.hidden {
	display: none;
	transition: 0.5s;
}
h1 {
	text-align: center;
	margin-bottom: 25px;
	text-transform: uppercase;
	color: #fff;
	letter-spacing: 5px;
}
input {
	background-color: transparent;
	width: 70%;
	color: #fff;
	border: none;
	border-bottom: 1px solid #fff;
	padding: 10px 0;
	text-indent: 8px 0;
	margin: 8px 0;
	font-size: 14px;
	letter-spacing: 2px;
}
input::placeholder {
	color: #fff;
	letter-spacing: 2px;
}
input:focus {
	outline: none;
	color: #a262ad;
	border-bottom: 1px solid #a262ad 80%;
	transition: 0.5s;
}
.form-box button {
	width: 70%;
	background-color: #f6f6f6;
	color: #a262ad;
	margin-top: 35px;
	outline: none;
	border-radius: 8px;
	padding: 13px;
	letter-spacing: 2px;
	border: none;
	cursor: pointer;
}
.con-box {
	width: 50%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
.con-box.left {
	left: -2%;
}
.con-box.right {
	right: -2%;
}
.con-box h2 {
	color: 8e9aaf;
	font-size: 25px;
	font-weight: bold;
	letter-spacing: 3px;
	text-align: center;
	margin-bottom: 4px;
}
.con-box p {
	color: #8e9aaf;
	font-size: 12px;
	letter-spacing: 2px;
	align-items: center;
}
.con-box span {
	color: #d3b7d8;
}

.con-box button {
	margin-top: 3%;
	background-color: #f6f6f6;
	color: #a262ad;
	border: 1px solid #d3b7d8;
	padding: 6px 10px;
	border-radius: 5px;
	letter-spacing: 1px;
	outline: none;
	cursor: pointer;
}
.con-box button:hover {
	background-color: #d3b7d8;
	color: #fff;
}
img {
	width: 40px;
	height: 40px;
}
</style>